package com.mdh.runerrands.controller;

import com.alibaba.fastjson.JSON;
import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.BaseUser;
import com.mdh.runerrands.pojo.Comment;
import com.mdh.runerrands.pojo.OrderLine;
import com.mdh.runerrands.pojo.QueryPage;
import com.mdh.runerrands.service.BaseUserService;
import com.mdh.runerrands.service.ICommentService;
import com.mdh.runerrands.service.OrderLineService;
import com.mdh.runerrands.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/controller/comment")
@RestController
public class CommentController {

    @Resource
    private ICommentService commentService;
    @Resource
    private OrderService orderService;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private OrderLineService orderLineService;

    @ApiOperation("批量删除评论信息")
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public Result batchDelete(int ...ids){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = commentService.batchDelete(ids);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation("评论信息通过审核")
    @RequestMapping(value = "/commentExamine",method = RequestMethod.GET)
    public Result commentExamine(int commentId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            String status = "审核通过";
            boolean b = commentService.updateComment(commentId, status);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation("评论信息审核不通过")
    @RequestMapping(value = "/commentRefuseExamine",method = RequestMethod.GET)
    public Result commentRefuseExamine(int commentId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            String status = "审核不通过";
            boolean b = commentService.updateComment(commentId, status);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("通过ID删除评论")
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = commentService.deleteById(id);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("根据顾客ID查询该顾客的所有评论信息")
    @RequestMapping(value = "/findCommentByCus",method = RequestMethod.GET)
    public Result findCommentByCus(int cusId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            List<Comment> byCusId = commentService.findByCusId(cusId);
            List list = new ArrayList();
            for (Comment comment :byCusId){
                Map map = new HashMap();
                map.put("id",comment.getId());
                map.put("content",comment.getContent());
                map.put("commentTime",comment.getCommentTime());
                map.put("orderId",comment.getOrderId());
                map.put("cusId",comment.getCusId());
                map.put("status",comment.getStatus());
                map.put("cusName",baseUserService.findBaseUserById(comment.getCusId()).getUsername());
                list.add(map);
            }
            Map map = new HashMap();
            map.put("total",byCusId.size());
            map.put("list", list);
            result.setMessage("success");
            result.setData(map);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation("分页查询评论信息")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Result query(int page,int pageSize,String content,Long commentTime,Integer cusId,Integer orderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            QueryPage queryPage = new QueryPage();
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            queryPage.setList(commentService.query(page,pageSize,content,commentTime,cusId,orderId));
            queryPage.setTotal(commentService.selectCount(content,commentTime,cusId,orderId));
            result.setData(queryPage);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("保存或更新")
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Result saveOrUpdate(Comment comment){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            if (comment!= null && comment.getId() != 0){
                boolean update = commentService.update(comment);
                result.setData(update);
                result.setMessage("success");
                return result;
            }else {
                int orderId = comment.getOrderId();
                orderService.updateRemark(orderId,"1");
                boolean insert = commentService.insert(comment);
                result.setData(insert);
                result.setMessage("success");
                return result;
            }
        }catch (Exception e){
            result.setData(JSON.toJSONString(e));
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("根据产品id查询它的所有评论")
    @RequestMapping(value = "/findCommentByProductId",method = RequestMethod.GET)
    public Result findCommentByProductId(Integer productId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        List list = new ArrayList();
        try {
            Map resultMap = new HashMap();
            List<OrderLine> byProductId = orderLineService.findByProductId(productId);
            System.out.println(JSON.toJSONString(byProductId));
            for (OrderLine orderLine :byProductId){
                List<Comment> byOrderId = commentService.findByOrderId(orderLine.getOrderId());
                if (byOrderId != null && byOrderId.size()!=0){
                    for (Comment comm :byOrderId){
                        BaseUser baseUserById = baseUserService.findBaseUserById(comm.getCusId());
                        Map map = new HashMap();
                        map.put("customerName",baseUserById.getUsername());
                        map.put("commentTime",comm.getCommentTime());
                        map.put("content",comm.getContent());
                        map.put("status",comm.getStatus());
                        list.add(map);}

                }
            }
            resultMap.put("total",list.size());
            resultMap.put("list",list);
            result.setData(resultMap);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(JSON.toJSONString(e));
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
}
